<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Topic;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
class TopicController extends Controller
{
    //
    public function topic(){
        $data = DB::table('topic')
            ->join('manager', 'manager.id', '=', 'topic.user_id')
            ->join('cate', 'cate.id', '=', 'topic.cate_id')
            ->where('topic.TFlag','<>',0)
            ->select('topic.*', 'manager.name', 'cate.cate_name')
            ->paginate(10);


//        dd($cate_d);
        return view('admin.topic.topic',compact('data'));
    }
    public function del(Request $request){
        $data =  $request->route( 'id' );
        Topic::where('id',$data)->update(['TFlag'=>0]);
        return json_encode(['message'=>'删除成功']);
    }
    public function all_del(Request $request){
        $data =  $request->all();
        foreach ($data['id'] as $val){
//            echo $val.'<br>';
            DB::table('topic')->where('id',$val)->update(['TFlag'=>0]);

        } return json_encode(['message'=>'删除成功']);

    }
    public function edit(Request $request){
        $data =  $request->route( 'id' );
        $data =  DB::table('topic')
            ->join('cate', 'cate.id', '=', 'topic.cate_id')
            ->select('topic.*', 'cate.cate_name')
            ->where('topic.id',$data)
            ->get();
        $cate_d = DB::table('cate')->get();
        return view('admin/topic/edit',compact('data','cate_d'));
    }
    public function upload(Request $request ){
        // 设置超时时间为没有限制
        ini_set("max_execution_time", "0");

        $http_type = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $file = $request->file('file');

        $allowed_extensions = ["png", "jpg", "gif", "jpeg", "bmp"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return json_encode(['error' => 'You may only upload png, jpg or gif or jpeg or bmp.']);
        }

        $destinationPath = 'storage/uploads/'.date('Ymd').'/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = md5(microtime(true)).'.'.$extension;
        $file->move($destinationPath, $fileName);
        Cookie::queue('RPath', $destinationPath.$fileName, 5);
    }
    public function edit_up( Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            $user_img = Cookie::get('RPath');
//            dd($data);
            unset($data['_token']);
            if (!empty($user_img)){

                DB::table('topic')->where('id','=',$data['id'])
                    ->update(
                        [
                            'title'=>$data['title'],
                            'content'=>$data['content'],
                            'cate_id'=>$data['cate_id'],
                            'TFlag'=>$data['TFlag'],
                            'RPath'=>Cookie::get('RPath'),
                        ]
                    );
                Cookie::queue('RPath',null,-1);
                return json_encode(['message'=>'修改成功1']);
            }else{

                DB::table('topic')->where('id','=',$data['id'])
                    ->update(
                        [
                            'title'=>$data['title'],
                            'content'=>$data['content'],
                            'cate_id'=>$data['cate_id'],
                            'TFlag'=>$data['TFlag'],
                        ]
                    );
                return json_encode(['message'=>'修改成功']);
            }
        }

    }
    public function add(){
        $cate_d = DB::table('cate')->get();
        return view('admin/topic/add',compact('cate_d'));
    }
    public function add_up(Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            $data['user_id'] = Auth::guard('admin') -> user() -> id;
//            dd($data);
            unset($data['_token']);
            $data['RPath'] = Cookie::get('RPath');

            $data['created_at'] = date('Y-m-d H:i:s');
            DB::table('topic')->insert($data);
            Cookie::queue('RPath',null,-1);
            return json_encode(['message'=>'添加成功']);
        }
    }
}
